﻿using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.Domains.ScratchTheFat;
using Traceability.Commont.DTOs;
using static Butcher.winform.EnterpriseForms.ScaleBackfill;
using static Butcher.winform.EnterpriseForms.WhiteNoteList;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Butcher.winform.EnterpriseForms.DivisionStorage
{
    public partial class DivisionStorageAdd : Form
    {
        public DivisionStorageAdd()
        {
            InitializeComponent();
            getList();
            textBox2.LostFocus += new EventHandler(textBox2_GotFocus);
            GetAllType();
            GetShopping();
        }

        public void GetAllType()
        {
            UIFlowLayoutPanel[] appro = new UIFlowLayoutPanel[5];
            for (int t = 0; t < appro.Length; t++)
            {
                UIFlowLayoutPanel uiflow = new UIFlowLayoutPanel();
                uiflow.Size = new Size(120, 50);
                Label newlabel = new Label();
                newlabel.Width = 80;
                newlabel.Height = 30;
                if (t == 0)
                {
                    newlabel.Text = "鲜段体";

                }
                else if (t == 1)
                {
                    newlabel.Text = "鲜分割";
                }
                else if (t== 2)
                {
                    newlabel.Text = "冻分割";
                }
                else if (t== 3)
                {
                    newlabel.Text = "冻骨";
                }
                else
                {
                    newlabel.Text = "鲜骨";
                }
                uiflow.Controls.Add(newlabel);
                uiFlowLayoutPanel1.Controls.Add(uiflow);
            }
        }
        int ShoppingDesignationids = 0;
        public void GetShopping()
        {
            Shopping shopping = new Shopping();
            string url = @"http://localhost:5229/api/Batch/getShop";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Shopping>(shopping));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Shopping> enterprises = aaa.ToObject<List<Shopping>>();
            System.Windows.Forms.Button[] buttons = new System.Windows.Forms.Button[enterprises.Count];
            for (int i = 0; i < buttons.Length; i++)
            {
                buttons[i] = new System.Windows.Forms.Button();
                //buttons[i].Text = ssss[i].DuctionName; /*(i + 1).ToString();*/
                buttons[i].Location = new Point(i * 130 + 130, 30);
                Shopping asas = enterprises[i];
                //转换为字符串
                buttons[i].Name = Json.Serialize(asas.ShoppingDesignationid);
                //转换为实体
                Shopping babab = Json.Deserialize<Shopping>(buttons[i].Name);
                buttons[i].Size = new Size(100, 80);
                buttons[i].BackColor = Color.White;
                buttons[i].ForeColor = Color.Black;
                //ScaleName.Text = ssss[i].ScaleName;
                buttons[i].Text = enterprises[i].ShoppingDesignationName;
                buttons[i].Click += new System.EventHandler(this.button_Click);
                uiFlowLayoutPanel2.Controls.Add(buttons[i]);
            }
        }

        private void button_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Button button = (System.Windows.Forms.Button)sender;
            ShoppingDesignationids = Convert.ToInt32(button.Name);
            MessageBox.Show(button.Name);
        }

        Scale ScaleInfo = new Scale();
        public static int ids;
        /// <summary>
        /// 选择磅秤
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiSymbolButton2_Click(object sender, EventArgs e)
        {
            this.Hide();
            ScaleBackfill.pid = 6;
            ScaleBackfill scale = new ScaleBackfill();
            scale.Owner = this;
            scale.Hide();
            scale.ShowDialog();
            Application.ExitThread();
        }
        /// <summary>
        /// 页面首次加载
        /// </summary>
        public void getList()
        {
            var strValue = ScaleBackfill.TxtVal;
            if (!string.IsNullOrEmpty(strValue))
            {
                ScaleInfo = Json.Deserialize<Scale>(strValue);
                textBox3.Text = ScaleInfo.TareWeightDeduction.ToString();
                uiSymbolButton2.Text = ScaleInfo.ScaleName;
                getStash(ScaleInfo.Warehouse);
            }
        }
        /// <summary>
        /// 加载仓库下拉框
        /// </summary>
        /// <param name="id"></param>
        public void getStash(int id)
        {
            Stash stash = new Stash();
            string url = @"http://localhost:5229/api/Batch/StashList";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<Stash>(stash));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var saaa = result;
            var aaa = info["date"].Value<JArray>();
            List<Stash> enterprises = aaa.ToObject<List<Stash>>();
            enterprises = enterprises.Where(x => x.Stashid == id).ToList();
            this.cb_Level.DataSource = enterprises;
            this.cb_Level.DisplayMember = "StashName";
            this.cb_Level.ValueMember = "Stashid";
        }
        /// <summary>
        /// 皮重失去焦点啊事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void textBox2_GotFocus(object sender, EventArgs e)
        {
            if (textBox3.Text != null)
            {
                textBox4.Text = (Convert.ToDecimal(textBox2.Text) - Convert.ToDecimal(textBox3.Text)).ToString();
                //textbox41 = ui_NetWeight.Text;
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            SplitRU split = new SplitRU();
            split.DocumentsDate = DateTime.Now;
            split.DocumentNo = "";
            split.ScaleId = ScaleInfo.Scaleid;
            split.RepositoryID = Convert.ToInt32(cb_Level.SelectedValue);
            split.WeightNum = Convert.ToInt32(textBox5.Text);
            split.GrossWeight = Convert.ToDecimal(textBox2.Text);
            split.Tare = Convert.ToDecimal(textBox3.Text);
            split.NetWeight = Convert.ToDecimal(textBox4.Text);
            split.ShoppIngId = ShoppingDesignationids;
            string url = @"http://localhost:5229/api/Ending/SplitRUCreate";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<SplitRU>(split));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            if (info != null)
            {
                MessageBox.Show("新增成功");
                thisList();
            }
        }
        public class pageDTO
        {
            public int pageindex { get; set; }

            public int pagesize { get; set; }
        }
        private void DivisionStorageAdd_Load(object sender, EventArgs e)
        {
            thisList();
        }
        public void thisList()
        {
            pageDTO page = new pageDTO();
            page.pageindex = 1;
            page.pagesize = 8;
            string url = @"http://localhost:5229/api/Ending/SplitRUHtml";
            HttpClient client = new HttpClient();
            var content = new StringContent(JsonSerializer.Serialize<pageDTO>(page));
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            JObject info = JObject.Parse(result);
            var aaa = info["lists"].Value<JArray>();
            List<SplitRU> enterprises = aaa.ToObject<List<SplitRU>>();
            dataGridView1.DataSource = enterprises;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var l = e.ColumnIndex;
            if (l == 2)
            {
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
                NEWDicsionStorage list = new NEWDicsionStorage();
                list.splitWarehousingId = Convert.ToInt32(row.Cells["序号"].Value);
                HttpClient client = new HttpClient();
                string url = @" http://localhost:5229/api/Ending/SplitRUDel";
                var content = new StringContent(JsonSerializer.Serialize<NEWDicsionStorage>(list), Encoding.UTF8);
                content.Headers.Remove("Content-Type");
                content.Headers.Add("Content-Type", "application/json");
                string result = client.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
                JObject josnresult = JObject.Parse(result);
                var results = josnresult["code"];


                if (results != null)
                {
                    MessageBox.Show("删除成功");
                    getList();
                }
            }
            else if (l == 1)
            {
                DataGridViewRow row = dataGridView1.Rows[e.RowIndex];

                ids = Convert.ToInt32(row.Cells["序号"].Value);
                DivisionStorageUpdate gethtml = new DivisionStorageUpdate();

                gethtml.Show();

            }
        }


        public class NEWDicsionStorage
        {

            public int splitWarehousingId { get; set; }


        }
    }
}
